package com.zoo.buyor.wechat.handler;


import cn.hutool.core.util.StrUtil;
import com.zoo.buyor.wechat.adapter.TextBuilder;
import com.zoo.buyor.wechat.service.WxMsgService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Component
public class SubscribeHandler extends AbstractHandler {
    @Autowired
    WxMsgService wxMsgService;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) throws WxErrorException {

        this.logger.info("新关注用户 OPENID: " + wxMpXmlMessage.getFromUser());
        String eventKey = wxMpXmlMessage.getEventKey();
        if (StrUtil.isBlank(eventKey)){
            return TextBuilder.build("欢迎关注",wxMpXmlMessage);
        }else {
            return wxMsgService.scanQRCodeWithArgument(wxMpXmlMessage);
        }

    }


}
